<template>
  <div class="sku-form">
    <!-- :model="spu"-->
    <el-form ref="form" label-width="80px">

      <el-form-item label="SPU名称">
        {{ spu.spuName }}
      </el-form-item>
      <el-form-item label="SKU名称">
        <el-input v-model="skuInfo.skuName" placeholder="SKU名称"/>
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input v-model="skuInfo.price" placeholder="价格(元)" type="number"/>
      </el-form-item>
      <el-form-item label="重量(千克)">
        <el-input v-model="skuInfo.weight" placeholder="重量(千克)"/>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input v-model="skuInfo.skuDesc" type="textarea" rows="4" placeholder="规格描述"/>
      </el-form-item>

      <el-form-item label="平台属性">
        <el-form :inline="true" ref="form" label-width="80px">
          <el-form-item :label="attr.attrName" v-for="attr in attrInfoList" :key="attr.id">
            <el-select v-model="attr.attrIdAndValueId" placeholder="请选择">
              <el-option
                  :value="`${attr.id}:${attrValue.id}`"
                  :label="attrValue.valueName"
                  v-for="attrValue in attr.attrValueList"
                  :key="attrValue.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>

      <el-form-item label="销售属性">
        <el-form :inline="true" ref="form" label-width="80px">
          <el-form-item :label="item.saleAttrName" v-for="item in spuSaleAttrList" :key="item.id">
            <el-select v-model="item.attrIdAndValueId" placeholder="请选择">
              <el-option
                  v-for="itemValue in item.spuSaleAttrValueList"
                  :key="itemValue.id"
                  :value="`${item.id}:${itemValue.id}`"
                  :label="itemValue.saleAttrValueName"
              >

              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>

      <el-form-item label="图片列表">
        <el-table
            style="width: 100%"
            border
            :data="spuImageList"
            @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" align="center" width="80"/>

          <el-table-column prop="saleAttrName" label="图片" width="width">
            <template slot-scope="{row}">
              <img :src="row.imgUrl" alt="" style="width: 100px;height: 100px;">
            </template>
          </el-table-column>
          <el-table-column prop="imgName" label="名称" width="width"/>
          <el-table-column prop="saleAttrName" label="操作" width="width">
            <template slot-scope="{row}">
              <el-button v-if="row.isDefault === 0" type="primary" size="mini" @click="changeDefault(row)">设置默认
              </el-button>
              <el-button v-else size="mini" type="success">默认</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="save">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>

    </el-form>
  </div>
</template>

<script>
export default {
  name: 'SkuForm',
  data() {
    return {
      spuImageList: [],
      spuSaleAttrList: [],
      attrInfoList: [],
      // 收集sku数据的字段
      skuInfo: {
        // 第一类收集的数据， 父组件给的
        category3Id: 0,
        spuId: 0,
        tmId: 0,
        // 第二类通过数据双向绑定收集
        skuName: '',
        price: 0,
        weight: '',
        skuDesc: '',
        // 第三类: 需要书写代码
        skuDefaultImg: '', // 默认图片
        skuImageList: [ // 收集图片的字段
          // {
          //   id: 0,
          //   imgName: '',
          //   imgUrl: '',
          //   isDefault: '',
          //   skuId: 0,
          //   spuImgId: 0
          // }
        ],
        skuAttrValueList: [ // 平台属性
          // {
          //   attrId: 0,
          //   valueId: 0
          // }
        ],
        skuSaleAttrValueList: [ // 销售属性
          // {
          //   id: 0,
          //   saleAttrId: 0,
          //   saleAttrName: '',
          //   saleAttrValueId: 0,
          //   saleAttrValueName: '',
          //   skuId: 0,
          //   spuId: 0
          // }
        ]
      },
      spu: {},
      // 收集图片的数据
      imageList: []
    }
  },
  methods: {
    async getData(category1Id, category2Id, spu) {
      // 收集父组件给的数据
      this.skuInfo.category3Id = spu.category3Id
      this.skuInfo.spuId = spu.id
      this.skuInfo.tmId = spu.tmId
      this.spu = spu

      // 获取图片的数据
      const result = await this.$API.spu.reqSpuImageList(spu.id)
      if (result.code === 200) {
        const list = result.data
        list.forEach(item => {
          item.isDefault = 0
        })
        this.spuImageList = list
      }

      // 获取销售属性的数据
      const result1 = await this.$API.spu.reqSpuSaleAttrList(spu.id)
      this.spuSaleAttrList = result1.data

      // 获取平台属性的数据
      const result2 = await this.$API.spu.reqAttrInfoList(category1Id, category2Id, spu.category3Id)
      this.attrInfoList = result2.data
    },
    // table表格复选框按钮的事件
    handleSelectionChange(parmas) {
      // 获取到用户选中图片的信息收集
      this.imageList = parmas;
    },
    // 排他操作
    changeDefault(row) {
      this.spuImageList.forEach(item => {
        item.isDefault = 0
      })
      row.isDefault = 1
      // 收集默认图片的地址
      this.skuInfo.skuDefaultImg = row.imgUrl
    },
    // 取消按钮
    cancel() {
      this.$emit('changeScenes', 0)
      // 清除数据
      Object.assign(this._data, this.$options.data())
    },
    // 保存按钮的事件
    async save() {
      // 整理参数
      // 平台属性
      const {attrInfoList, skuInfo, spuSaleAttrList, imageList} = this

      // 整理平台数据的数据方式1
      // const arr = []
      // attrInfoList.forEach(item => {
      //   // 代表选择了平台属性
      //   if (item.attrIdAndValueId) {
      //     const [attrId, valueId] = item.attrIdAndValueId.split(':')
      //     const obj = {attrId, valueId}
      //     arr.push(obj)
      //   }
      // })
      // // 整理好参数赋值给skuInfo.skuAttrValueList
      // skuInfo.skuAttrValueList = arr

      // 整理平台数据的数据方式2
      skuInfo.skuAttrValueList = attrInfoList.reduce((prev, item) => {
        if (item.attrIdAndValueId) {
          const [attrId, valueId] = item.attrIdAndValueId.split(':')
          prev.push({attrId, valueId})
        }
        return prev
      }, [])
      // 整理销售数据
      skuInfo.skuSaleAttrValueList = spuSaleAttrList.reduce((prev, item) => {
        if (item.attrIdAndValueId) {
          const [saleAttrId, saleAttrValueId] = item.attrIdAndValueId.split(":");
          prev.push({saleAttrId, saleAttrValueId})
        }
        return prev
      }, [])
      // 整理图片数据
      skuInfo.skuImageList = imageList.map(item => {
        return {
          imgName: item.imgName,
          imgUrl: item.imgUrl,
          isDefault: item.isDefault,
          spuImageId: item.id
        }
      })

      const result = await this.$API.spu.reqAddSku(skuInfo)
      if (result.code === 200) {
        this.$message({type:'success',message: '保存成功'})
        this.$emit('changeScenes', 0);

      }

    }
  }
}
</script>

<style scoped>

</style>
